﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MobiMenu.Restaurant.Web.Utility;
using System.Collections.Specialized;

namespace MobiMenu.Restaurant.Web
{
    /// <summary>
    ///RESTContext 的摘要说明
    /// </summary>
    public class RESTContext<T>
    {
        public RESTContext(HttpContext context)
        {
            Init(context);
        }

        private void Init(HttpContext context)
        {
            HttpContext = context;
            switch (context.Request.HttpMethod.ToUpper())
            {
                case "HEAD":
                    HttpMethodType = HttpMethod.Head;
                    break;
                case "GET":
                    HttpMethodType = HttpMethod.Get;
                    break;
                case "POST":
                    HttpMethodType = HttpMethod.Post;
                    break;
                case "PUT":
                    HttpMethodType = HttpMethod.Put;
                    break;
                case "DELETE":
                    HttpMethodType = HttpMethod.Delete;
                    break;
                default:
                    HttpMethodType = HttpMethod.Unkown;
                    break;
            }

            InitRESTRequestData();
            ResponseData = new ResponseData<T>();
        }

        public HttpContext HttpContext
        {
            get;
            private set;
        }

        public ResponseData<T> ResponseData
        {
            get;
            private set;
        }

        public RequestData<T> RequestData
        {
            get;
            private set;
        }

        public HttpMethod HttpMethodType
        {
            get;
            private set;
        }

        public void InitRESTRequestData()
        {
            switch (HttpMethodType)
            { 
                case HttpMethod.Post:
                    RequestData = new RequestData<T>(HttpContext.Request.Params);
                    return;
                case HttpMethod.Get:
                    RequestData = new RequestData<T>(HttpContext.Request.QueryString);
                    return;
            }
        }
    }
}